package JUC;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        T1 t1 =new T1();
        // FutureTask类是Runnable接口的一个实现类，需要传入实现了Callable接口的对象
        FutureTask<Integer> ft = new FutureTask<>(t1);
        // Thread类传入实现了Runnable接口的类，调用start()开启线程
        new Thread(ft).start();
        // FutureTask.get()获取返回值并抛出异常(两个特点)
        Integer integer = ft.get();
        System.out.println(Thread.currentThread().getName()+integer);
    }
}

// Callable有返回值，实现接口的时候要指定泛型，相当于指定返回值
class T1 implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return 111;
    }
}